package com.orange.core.util.function;

import java.util.function.Function;

@FunctionalInterface
public interface ThrowingFunction<T, R, E extends Exception> {

    static <T, R> Function<T, R> wrapper(ThrowingFunction<T, R, Exception> throwingFunction) {
        return t -> {
            try {
                return throwingFunction.apply(t);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        };
    }

    static <T, R, E extends RuntimeException> Function<T, R> wrapper(ThrowingFunction<T, R, Exception> throwingFunction, E ex) {
        return t -> {
            try {
                return throwingFunction.apply(t);
            } catch (Exception e) {
                ex.initCause(e);
                throw ex;
            }
        };
    }

    R apply(T t) throws E;
}
